import 'dart:convert';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_echarts/flutter_echarts.dart';
import 'package:ls_work/util/ext_utils.dart';

import 'chart_data.dart';


class ChartWidget {
  static Widget lineChart(List<IChartData> list,
      {List<Color> colors = const [
        Color(0xff4B9CEF),
        Color(0xffB4D9FF),
      ],
      String format = "",
      bool showXAis = false,double? min,double? max}) {
    if (list.isEmpty) {
      return CommonText('无数据', textColor: Colors.orange)
          .addToContainer(height: 120, alignment: Alignment.center);
    }
    var map = {
      "tooltip": {
        "trigger": 'axis',
      },

      "yAxis": {
        "type": 'value',
        "min":min,
        "max":max
      },
      "grid": {"left": "10%", "top": "4%", "bottom": "10%"},
      "xAxis": {
        "type": 'category',
        "show": showXAis,
        "boundaryGap": false,
        "data":list.map((e) => e.chartName).toList()
      },
      "series": [
        {
          "data": list.map((e) => e.chartValue).toList(),
          "type": 'line',
          "smooth": true,
          "symbol": "none",

        }
      ]
    };
    return Echarts(option: jsonEncode(map))
        .addToContainer(height: list.length * 45.0);
  }
}
